# -*- coding: utf-8 -*-
'''
@author: Hugo
@file: RGB_test.py
@time: 2021/9/4 16:00
'''
from io import BytesIO

from PIL import Image,ImageDraw,ImageFont

'''
#首建长宽 200x100 像素  ,背景颜色为(26,255,255)的画布对象
image = Image.new('RGB',(400,400),(26,255,255))
#  新建绘画对象
draw = ImageDraw.Draw(image,mode = 'RGB')
font =  ImageFont.truetype(r"c:\windows\Fonts\Arial.ttf",80)   # 指定字体文件,字体大小
#  新建的对象的坐标(99,99像素)位置处开始画出文本
draw.text((190,190),"abcd",font=font,fill=(200,0,200))  # 指定文本的大小,内容,字体颜色
image.show()


import requests
with open('D:\TulingPaCong\day19\jingtian.jpg','wb') as f:
    f.write(requests.get(r'https://gimg2.baidu.com/image_search/src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fq_70%2Cc_zoom%2Cw_640%2Fimages%2F20200310%2F7a3cb2afeffa42d59363c6840e92d9c3.jpeg&refer=http%3A%2F%2F5b0988e595225.cdn.sohucs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633337187&t=663dc23e502dbfbf90c45a0f17072457').content)

'''

from PIL import Image,ImageDraw,ImageFont
import numpy

# im = Image.open('D:\TulingPaCong\day19\jingtian.jpg')
# arr = numpy.array(im)
# print(arr)
# convert()传的参数为数字1,输出为二值化土图片,convert()传的参数为字母L,输出为灰度图片
# im = Image.open('D:\TulingPaCong\day19\jingtian.jpg').convert('L')
# 改变图片大小
# ims = im.resize((120,120))
# 旋转角度
# ims = im.rotate(-30)
# print(im.size)
# print(im.mode)
# print(im.info)
# im.show()
# 裁剪图片
# imc = im.crop(box=[100,100,400,400])
# imc.show()
# 翻转图片
# im = Image.open('D:\TulingPaCong\day19\jingtian.jpg')
# img= im.transpose(Image.FLIP_TOP_BOTTOM)
# img.show()
# img.save('D:\TulingPaCong\day19\jingtian1.jpg')
#

import random
from PIL import Image, ImageDraw, ImageFont

def get_img():
    width, height, font_size, font_num = 300, 100, 48, 5
    bg_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0,
    255)) # 背景颜色
    image = Image.new(mode='RGB', size=(width, height), color=bg_color) # 画布
    draw = ImageDraw.Draw(image, mode='RGB')     # 绘图类
    font = ImageFont.truetype("C:\Windows\Fonts\Arial.ttf", font_size)  # 字体
    verify = str()
    for i in range(font_num):
       x = random.randint(i * (width / font_num), (i + 1) * (width / font_num) -
    font_size)
       y = random.randint(0, height - font_size)
       char = random.choice([chr(alpha) for alpha in range(65, 91)] + [str(num) for
    num in range(10)]) # 随机参数拼接给verify
       verify += char
       color = (random.randint(0, 255), random.randint(0, 255), random.randint(0,
    255))
       draw.text((x, y), char, fill=color, font=font)
    b_io = BytesIO()
    image.save(b_io, format = 'PNG')
    im_by = b_io.getvalue()  # 图片字节码
    return verify, im_by
    print(verify)
    # image.show()

get_img()























